/*
 * 百度在线网络技术（北京）有限公司拥有本软件版权2021并保留所有权利。
 * Copyright 2021, Baidu.com,Inc 2:Baidu Online Network Technology (Beijing) Co.,Ltd,
 * All rights reserved.
 */

package com.azdebugit.threapool.executor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.azdebugit.test.collection.CommandUtil;
import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadTest3 {
    private static Logger log = LoggerFactory.getLogger(ThreadTest3.class.getSimpleName());
    // 自定义线程池的未捕获异常处理程序
    private final static ThreadPoolExecutor executor = new ThreadPoolExecutor(8, 8,
            30, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(),
            new ThreadFactoryBuilder()
                    .setNameFormat("pool-%d")
                    .setUncaughtExceptionHandler((Thread t, Throwable e) -> {
                        log.error("pool happen exception, thread is {}", t, e);
                    }).build());

    /*  ThreadPoolExecutor executor = new ThreadPoolExecutor(8, 8,
                           30, TimeUnit.MINUTES,
                           new LinkedBlockingQueue<>(),
                           new ThreadFactoryBuilder()
                           .setNameFormat("pool-%d")
                           .setUncaughtExceptionHandler((Thread t, Throwable e) -> {
                                 log.error("pool happen exception, thread is {}", t, e);
                           })
                                       .build());*/
    // 设置全局的默认未捕获异常处理程序
    static {
        Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> {
            log.error("Thread {} got exception", thread, throwable);
        });
    }

    public static int aeger = 0;

    public static void main1(String[] args) {
        while (aeger < 1111) {
            log.error("-----" + Thread.currentThread());
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    log.error("####" + Thread.currentThread());
                    System.out.println(aeger++);
                    throw new RuntimeException("*****************");
                }
            });
        }
    }
}